package com.es.linklist;


/**
 * @author 二十
 * @since 2022/1/17 12:55 上午
 */
public class DummyLinkedList<E> extends AbstractLinkedList<E> {

    private class Node {
        public E e;
        public Node next;

        public Node(E e, Node node) {
            this.next = node;
            this.e = e;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return "Node{" +
                    "e=" + e +
                    '}';
        }
    }

    private int size;

    private Node head;

    public DummyLinkedList() {
        head = new Node();
    }

    @Override
    public void addFirst(E e) {
        addIndex(0, e);
    }

    @Override
    public void addIndex(int index, E o) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("error of args index!");
        }
        Node p = head;
        for (int i = 0; i < index; i++) {
            p = p.next;
        }
        p.next = new Node(o, p.next);
        size++;
    }

    @Override
    public void addTail(E o) {
        addIndex(size, o);
    }

    @Override
    public E get(int index) {
        Node p = head;
        for (int i = 0; i < index; i++) {
            p = p.next;
        }

        return p.next.e;
    }

    @Override
    public E getFirst() {
        return head.next.e;
    }

    @Override
    public E getTail() {
        Node p = head;
        for (int i = 0; i < size; i++) {
            p = p.next;
        }
        return p.next.e;
    }

    @Override
    public boolean contains(E o) {
        Node p = head;
        for (int i = 0; i < size; i++) {
            if (p.next.e.equals(o)) {
                return true;
            }
            p = p.next;
        }
        return false;
    }

    @Override
    public void set(int index, E o) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("error of args index!");
        }
        Node p = head;
        for (int i = 0; i < index; i++) {
            p = p.next;
        }
        p.next.e = o;
    }

    @Override
    public void deleteNode(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("error of args index!");
        }
        Node p = head;
        for (int i = 0; i < index; i++) {
            p = p.next;
        }
        p.next = p.next.next;
        size--;
    }

    @Override
    public void deleteFirst() {
        deleteNode(0);
    }

    @Override
    public void deleteLast() {
        deleteNode(size - 1);
    }

    @Override
    public void deleteElement(E e) {
        Node p = head;
        while (p.next != null) {

            if (p.next.e.equals(e)) {
                p.next = p.next.next;
                size--;
            } else {
                p = p.next;
            }
        }
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("LinkedList:[");
        Node p = head;
        for (int i = 0; i < size; i++) {
            sb.append(p.next.e);
            p = p.next;
            if (i != size - 1) {
                sb.append(",");
            }
        }
        return sb.append("]").toString();
    }
}
